package jcataloger.renderers;


import java.awt.Component;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

import jcataloger.beans.SearchResultBean;
import jcataloger.resources.Parameters;

/**
 * This class is used to render JTable components in the project. It is used for showing <br>
 * images near items in the table according to their types.
 * @author sekobey
 *
 */
public class JCatalogerTableCellRenderer extends JLabel implements TableCellRenderer {
	
	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
		
		ClassLoader loader = this.getClass().getClassLoader();
		ImageIcon folderIcon = new ImageIcon(loader.getResource(Parameters.getString("icons22x22.folder")));
		ImageIcon fileIcon = new ImageIcon(loader.getResource(Parameters.getString("icons22x22.file")));
		
		if ( value instanceof SearchResultBean ) {
			SearchResultBean item = (SearchResultBean)value;
			setText( item.getFileName() );
			setIcon( item.getType().equals("file") ? fileIcon : folderIcon );
		}
		if (isSelected) {
			setBackground(table.getSelectionBackground());
			setForeground(table.getSelectionForeground());
		}
		else {
			setBackground(table.getBackground());
			setForeground(table.getForeground());
		}
		setEnabled(table.isEnabled());
		setFont(table.getFont());
		setOpaque(true);
		return this;
	}

}
